//
//  ZYEmoticonBottomView.swift
//  WBBeta1
//
//  Created by wustzhy on 16/8/18.
//  Copyright © 2016年 Yestin. All rights reserved.
//

import UIKit

let tagNumber = 2000
// 按钮枚举
enum ZYEmoticonBottomViewType: Int {
    // 默认
    case Default = 0
    // emoji
    case Emoji = 1
    // 浪小花
    case Lxh = 2
}

//MARK: ---UIStackView----ios9.0
class ZYEmoticonBottomView: UIStackView {
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func setupUI() {
        //水平填充
        axis = .Horizontal
        //填充方式 - 分布
        distribution = .FillEqually
        
        addChildButtons("left", title: "默认",type: .Default)
        addChildButtons("mid", title: "Emoji",type: .Emoji)
        addChildButtons("right", title: "浪小花",type: .Lxh)
        
    }
    
    func addChildButtons(imgName: String, title: String, type: ZYEmoticonBottomViewType) -> UIButton{
        let button = UIButton()
        button.tag = type.rawValue + tagNumber
        // 添加事件
        button.addTarget(self, action: "buttonClick:", forControlEvents: UIControlEvents.TouchUpInside)
        button.setBackgroundImage(UIImage(named: "compose_emotion_table_\(imgName)_normal"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "compose_emotion_table_\(imgName)_selected"), forState: UIControlState.Selected)
        button.setTitle(title, forState: UIControlState.Normal)
        button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Selected)
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        
        //默认第一个按钮处于选中
        if type == .Default {
            button.selected = true
            currentSelButton = button
        }
        
        // 添加按钮
        addArrangedSubview(button)
        return button
    }
    var currentSelButton:UIButton?
    func buttonClick(btn:UIButton) {
        onlySelOneButton(btn)
                closureSelEmoticonBtn?(type: ZYEmoticonBottomViewType(rawValue: btn.tag - tagNumber)!)
    }
    var closureSelEmoticonBtn:((type:ZYEmoticonBottomViewType)->())?
    
    func onlySelOneButton(btn:UIButton) {
        if currentSelButton == btn {
            return
        }
        currentSelButton?.selected = false
        btn.selected = true
        currentSelButton = btn

    }
    //来自 键盘view 手动滑动cell来设置 按钮被点击状态
    func setupCurrentBtnFromKB(tag: Int) {
        let btn = viewWithTag(tag + tagNumber) as! UIButton //tag == 0 崩溃
        onlySelOneButton(btn)
    }
    
    
    
}
